//
//  HEHTMLStringViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/2/9.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEHTMLStringViewController.h"
#import <RZColorful/RZColorful.h>

@interface HEHTMLStringViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;

// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) NSMutableAttributedString *attrStr;

@end

@implementation HEHTMLStringViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    self.title = @"团队详请";
    
    [self dealStr];
    [self initUI];
}
  
- (void)dealStr {
    NSMutableAttributedString *attr = [NSAttributedString htmlString: _htmlStr].mutableCopy;

    // 图片的尺寸，在转换保存的时候，是保存的图片本身的size（px），在转换为富文本的时候，并没有转为pt，所以会有可能变大并别裁剪，这里做一个尺寸适配
    NSArray <RZAttributedStringInfo *> *array = [attr rz_attributedStringByAttributeName:NSAttachmentAttributeName];
    [array enumerateObjectsUsingBlock:^(RZAttributedStringInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSTextAttachment *att = obj.value;
        UIImage *image = att.image;
        if (image == nil) {
            image = [UIImage imageWithData:att.fileWrapper.regularFileContents];
        }
        CGFloat width = MIN(image.size.width, KScreenWidth - 20);
        CGFloat height = MIN(image.size.height, image.size.height * width / image.size.width);
        CGRect bounds = att.bounds;
        bounds.size = CGSizeMake(width, height);
        NSAttributedString *temp = [NSAttributedString rz_colorfulConfer:^(RZColorfulConferrer *confer) {
            confer.image(image).bounds(bounds);
        }];
        [attr replaceCharactersInRange:obj.range withAttributedString:temp];
    }];
    [attr addAttributes:@{NSFontAttributeName: DYFont(15)} range:NSMakeRange(0, attr.length)];
    _attrStr = attr;
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
    
    
    // 描述
    _textView = [UITextView new];
    _textView.attributedText = _attrStr;
    _textView.editable=NO;
    _textView.scrollEnabled = NO;
    _textView.textColor = CNavLightBlackColor;
    _textView.font = DYFont(16);
    _textView.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:_textView];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
        make.bottom.equalTo(self.contentView).mas_offset(-10);
    }];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
//        make.height.greaterThanOrEqualTo(@0.0f);
    }];
}
 

#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}


@end
